# THIS SCRIPT COMPUTES SUMMARY STATISTICS FOR FIGURE 7, AND PROVIDES
# THE SYNTAX FOR CREATING THE FIGURE ITSELF. IT MUST BE RUN IN CONJUNCTION 
# WITH THE DATASET "ANESCumulative_cleaned.csv", AVAILABLE IN THE REPLICATION 
# MATERIALS ON DATAVERSE. 
rm(list = ls())
library(foreign)
library(car)
library(readstata13)
library(ggplot2)
library(stargazer)
library(grid)
library(gridExtra)
library(survey)
library(dplyr)
library(lemon)
library(ggpubr)
library(reshape2)

# READ IN THE DATA. SELECT THE FILE "ANESCumulative_cleaned.csv"
our.data = read.csv(file.choose())
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# GOVERNMENT SERVICES AND SPENDING

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# POSITION, GOVERNMENT SERVICES AND SPENDING, REPUBLICANS
our.means.republicans = svyby(~govspending, ~year + as.factor(programmatic), 
                              svymean, design = subset(our.design, republican==1), na.rm = TRUE)
our.means.republicans = as.data.frame(our.means.republicans)
# POSITION, GOVERNMENT SERVICES AND SPENDING, DEMOCRATS
our.means.democrats = svyby(~govspending, ~year + as.factor(programmatic), 
                            svymean, design = subset(our.design, democrat==1), na.rm = TRUE)
our.means.democrats = as.data.frame(our.means.democrats)
# BIND THE TWO DATASETS
means.data = rbind(our.means.republicans, our.means.democrats)
names(means.data)[2] = c("programmatic")
levels(means.data$programmatic) = c("Unmatched", "Partially matched", "Matched")
means.data$programmatic = factor(means.data$programmatic, levels = c("Matched",
                                                                     "Partially matched",
                                                                     "Unmatched"))
means.data$partisanship = c(rep("Republicans", 60), 
                            rep("Democrats", 60))
# YEAR
means.data$year = as.numeric(as.character(means.data$year))
# REMOVE EMPTY YEARS; CLEAN UP
means.data.spending = subset(means.data, govspending!=0)
means.data.spending$item = rep("Government services \nand spending", 
                               length(means.data.spending$year))
names(means.data.spending)[3] = "average"
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# GOVERNMENT HEALTH INSURANCE

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# POSITION, REPUBLICANS
our.means.republicans = svyby(~govhealth, ~year + as.factor(programmatic), 
                              svymean, design = subset(our.design, republican==1), na.rm = TRUE)
our.means.republicans = as.data.frame(our.means.republicans)
# POSITION, DEMOCRATS
our.means.democrats = svyby(~govhealth, ~year + as.factor(programmatic), 
                            svymean, design = subset(our.design, democrat==1), na.rm = TRUE)
our.means.democrats = as.data.frame(our.means.democrats)
# BIND THE TWO DATASETS
means.data = rbind(our.means.republicans, our.means.democrats)
names(means.data)[2] = c("programmatic")
levels(means.data$programmatic) = c("Unmatched", "Partially matched", "Matched")
means.data$programmatic = factor(means.data$programmatic, levels = c("Matched",
                                                                     "Partially matched",
                                                                     "Unmatched"))
means.data$partisanship = c(rep("Republicans", 60), 
                            rep("Democrats", 60))
# YEAR
means.data$year = as.numeric(as.character(means.data$year))
# REMOVE EMPTY YEARS; CLEAN UP
means.data.health = subset(means.data, govhealth!=0)
means.data.health$item = rep("Government health \ninsurance", 
                             length(means.data.health$year))
names(means.data.health)[3] = "average"
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# GOVERNMENT GUARANTEED JOBS

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# POSITION, REPUBLICANS
our.means.republicans = svyby(~govjobs, ~year + as.factor(programmatic), 
                              svymean, design = subset(our.design, republican==1), na.rm = TRUE)
our.means.republicans = as.data.frame(our.means.republicans)
# POSITION, DEMOCRATS
our.means.democrats = svyby(~govjobs, ~year + as.factor(programmatic), 
                            svymean, design = subset(our.design, democrat==1), na.rm = TRUE)
our.means.democrats = as.data.frame(our.means.democrats)
# BIND THE TWO DATASETS
means.data = rbind(our.means.republicans, our.means.democrats)
names(means.data)[2] = c("programmatic")
levels(means.data$programmatic) = c("Unmatched", "Partially matched", "Matched")
means.data$programmatic = factor(means.data$programmatic, levels = c("Matched",
                                                                     "Partially matched",
                                                                     "Unmatched"))
means.data$partisanship = c(rep("Republicans", 60), 
                            rep("Democrats", 60))
# YEAR
means.data$year = as.numeric(as.character(means.data$year))
# REMOVE EMPTY YEARS; CLEAN UP
means.data.jobs = subset(means.data, govjobs!=0)
means.data.jobs$item = rep("Government \nguaranteed jobs", 
                           length(means.data.jobs$year))
names(means.data.jobs)[3] = "average"
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# MERGE 
new.data = rbind(means.data.spending, 
                 means.data.health, 
                 means.data.jobs)
new.data

# FIGURE 7: AVERAGE ATTITUDES ON THE SIZE AND SCOPE OF GOVERNMENT, 1972-2020,
# BY PARTISANSHIP AND MATCHING STATUS. 
our.plot = ggplot(new.data,
                  aes(year, average, group = partisanship, 
                      linetype =  partisanship)) +
  geom_line(aes(linetype = partisanship), linewidth = 1) +
  labs(linetype = "Partisanship") + 
  xlab("Year") +
  ylab("Average \n") +
  # ggtitle("Average Attitudes on Size and Scope of Government, 1972-2020, \nBy Partisanship and Matching Status", 
  #        subtitle = "Data: ANES cumulative file, partisan identifiers only. \nDashed lines indicate the midpoint on the seven-point indices. \nAll variables rescaled between 0-1.") + 
  theme_minimal() +  
  # scale_color_grey() +
  facet_grid(programmatic ~ item, scales = "free_x") +
  # theme(legend.title = element_text(face = "bold")) +
  theme(legend.position = "none") +
  # theme(legend.title = element_text(size = 12)) +
  # theme(legend.text = element_text(size = 12)) +
  theme(axis.title.x = element_text(size = 9)) + 
  theme(axis.title.y = element_text(size = 9)) +
  # theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14)) + 
  # theme(plot.subtitle = element_text(hjust = 0.5, size = 12)) +
  theme(strip.text = element_text(face = "bold", size = 9)) + 
  geom_hline(yintercept = 0.5, linetype = "dashed") + ylim(0, 1) +
  theme(panel.spacing = unit(1, "lines"))
our.plot

# SAVE
# ggsave(our.plot, file = "Schmidtetal-Figure7.pdf", width = 6.6, height = 9)
